﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hec.TextMining;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace Tests
{
    [TestClass]
    public class TestDistances
    {
        //private IList<Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>> vsm;
        private IList<Hec.TextMining.KeyValuePair<Document, List<string>>> texts;

        public TestDistances()
        {

            texts = new List<Hec.TextMining.KeyValuePair<Document, List<string>>>();

            texts.Add(new Hec.TextMining.KeyValuePair<Document, List<string>>(
                    new Document("A") { ID = 1},
                    new List<string>() 
                    { 
                        "term1", "term1", "term1", "term1", "term1" , 
                        "term2", "term2", "term2", "term2", 
                        "term3"
                    }));

            texts.Add(new Hec.TextMining.KeyValuePair<Document, List<string>>(
                    new Document("B") {ID = 2},
                    new List<string>() 
                    { 
                        "term2", "term2", "term2", 
                        "term3", "term3", "term3", "term3", "term3", "term3",
                        "term4"
                    }));

            texts.Add(new Hec.TextMining.KeyValuePair<Document, List<string>>(
                    new Document("C"){ID=3},
                    new List<string>() 
                    { 
                        "term3", "term3", "term3","term3", "term3",
                        "term4","term4", "term4"
                    }));
        }

        [TestMethod]
        public void Test_TF_IDF()
        {
            var mining = new TextMining(texts);
            mining._5_rawFrequency_VectorSpaceModel(-1);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

            Assert.AreEqual(mining.VSM.Single(x => x.Key.FullName == "A").Value.Single(x => x.Key == "term1").Value.TF_IDF, 0.2385606);
            Assert.AreEqual(mining.VSM.Single(x => x.Key.FullName == "A").Value.Single(x => x.Key == "term2").Value.TF_IDF, 0.0704365);
        }

        [TestMethod]
        public void Test_calculations_TF_IDF()
        {
            var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\test_vsm_big.xml");
            var result = mySerializer.Deserialize();

            var mining = new TextMining(result); 
            
            var stopwatch = new Stopwatch();
            // Begin timing
            stopwatch.Start();
            mining._6_discriminant(0.3, 0.7);
            //mining._7_calculer_distances_TF_IDF();
            stopwatch.Stop();
        }

        [TestMethod]
        public void Test_discriminant()
        {
            #region Creation of test obect
            var test = new List<Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>>();
            var docA = new Document("A") { ID = 1 };
            var docB = new Document("B") { ID = 2 };
            var docC = new Document("C") { ID = 3 };
            var docD = new Document("D") { ID = 4 };

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docA,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(800, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(1, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(4, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(1, docA))

                    }));

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docB,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(0, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(400, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(4, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(700, docB))
                    }));

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docC,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(0, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(0, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(0, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(6, docC))
                    }));

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docD,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(0, docD)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(0, docD)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(0, docD)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(0, docD))
                    })); 
            #endregion

            var mining = new TextMining(test);
            mining._6_discriminant(0.3, 0.7);
        }

        [TestMethod]
        public void Test_Tf_Idf_With_Given_Example()
        {
            #region Creation of test obect
            var test = new List<Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>>();
            var docA = new Document("A") { ID = 1, TotalTermCount = 10 };
            var docB = new Document("B") { ID = 2, TotalTermCount = 10 };
            var docC = new Document("C") { ID = 3, TotalTermCount = 8 };

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docA,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(5, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(4, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(1, docA)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(0, docA))

                    }));

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docB,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(0, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(3, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(6, docB)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(1, docB))
                    }));

            test.Add(new Hec.TextMining.KeyValuePair<Document, List<Hec.TextMining.KeyValuePair<string, Cell>>>(
                    docC,
                    new List<Hec.TextMining.KeyValuePair<string, Cell>>() 
                    { 
                        new Hec.TextMining.KeyValuePair<string, Cell>("t1", new Cell(0, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t2", new Cell(0, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t3", new Cell(5, docC)),
                        new Hec.TextMining.KeyValuePair<string, Cell>("t4", new Cell(3, docC))
                    }));

            #endregion

            var mining = new TextMining(test);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

            Assert.AreEqual(
                Math.Round(mining.VSM.First().Value.First().Value.TF_IDF, 4), 
                0.2386);

            Assert.AreEqual(
                Math.Round(mining.VSM.Skip(1).First().Value.Skip(3).First().Value.TF_IDF, 4),
                0.0176);

            Assert.AreEqual(
                Math.Round(mining.VSM.First().Key.Distances.Single(x => x.Key.FullName == "B").Value.Euclidian_TF_IDF, 4),
                0.2399
                );

            Assert.AreEqual(
                Math.Round(mining.VSM.First().Key.Distances.Single(x => x.Key.FullName == "C").Value.Euclidian_TF_IDF, 4),
                0.2574
                );

        }

        [TestMethod]
        public void Test_Euclidian_distance_TF_IDF()
        {
            var mySerializer = new KeyValuePairSerializer(@"\MySerializedObjects\final_vsm_all.xml");
            var result = mySerializer.Deserialize();

            var mining = new TextMining(result);

            var stopwatch = new Stopwatch();

            // Begin timing
            stopwatch.Start();
            mining._6_discriminant(0.3, 0.7);
            mining._7_calculer_poids_tf_idf();
            mining._7_1_calculer_distances();

          stopwatch.Stop();
        }
    }
}
